<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库" prop="goodsId">
        <el-select v-model="queryParams.storeId" placeholder="请选择">
          <el-option v-for="item in allStoreList" :key="item.store_id" :label="item.store_name" :value="item.store_id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="货物来源类型" prop="goodsId">
        <el-select v-model="queryParams.goodsType" placeholder="请选择货物来源类型">
          <el-option key="00" label="农作物" value="0"></el-option>
          <el-option key="01" label="加工厂" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="goodsId">
        <el-select v-model="queryParams.status" placeholder="请选择货物来源类型">
          <el-option key="0" label="未签收" value="0"></el-option>
          <el-option key="1" label="已签收" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['source:in:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['source:in:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['source:in:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['source:in:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="storeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="入库编号" align="center" prop="inStoreId" />
      <el-table-column label="仓库名称" align="center" prop="storeName">
        <template slot-scope="scope">
          <router-link :to="'/source/store-go/index/' + scope.row.inStoreId" class="link-type">
            <span>{{scope.row.storeName}}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="入库数量" align="center" prop="inStoreNum" />
      <el-table-column label="货物名称" align="center" prop="goodsName" />
      <el-table-column label="货物批次号" align="center" prop="batchNum" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="签收状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status==='0'">未签收</span>
          <span v-if="scope.row.status==='1'">已签收</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="createBy" />
      <el-table-column label="入库时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSignFor(scope.row.inStoreId)"
          >签收</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改入库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="入库数量" prop="inStoreNum">
          <el-input v-model="form.inStoreNum" placeholder="请输入入库数量" />
        </el-form-item>
        <el-form-item label="货物id" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入货物id" />
        </el-form-item>
        <el-form-item label="货物来源" prop="goodsId">
          <el-select v-model="form.inStoreType" placeholder="请选择货物来源">
            <el-option key="0" label="农作物信息" value="0"></el-option>
            <el-option key="1" label="加工产品信息" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStore, getStore, delStore, addStore, updateStore,getStoreList,handleSignFor } from "@/api/source/store/in/store";

export default {
  name: "Store",
  data() {
    return {
      //仓库列表
      allStoreList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入库表格数据
      storeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeId:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    if (this.$route.params.storeId!=null){
      this.queryParams.storeId = parseInt(this.$route.params.storeId);
      console.log(this.queryParams.storeId);
    }else {
      this.queryParams.storeId = null;
      console.log(this.queryParams.storeId);
    }
    this.getStoreList();
    this.getList();
  },
  methods: {
    //确认签收
    handleSignFor(inStoreId){
      handleSignFor(inStoreId).then(res=>{
        this.getList();
      })
    },
    //查询所有仓库列表
    getStoreList(){
      getStoreList().then(res=>{
        this.allStoreList = res.rows;
      })
    },
    /** 查询入库列表 */
    getList() {
      this.loading = true;
      listStore(this.queryParams).then(response => {
        this.storeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        inStoreId: null,
        inStoreNum: null,
        goodsId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        isFalut: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams={
          pageNum: 1,
          pageSize: 10,
          storeId:this.$route.params.storeId
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.inStoreId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.storeId = this.$route.params.storeId;
      this.open = true;
      this.title = "添加入库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const inStoreId = row.inStoreId || this.ids
      getStore(inStoreId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改入库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.inStoreId != null) {
            updateStore(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStore(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const inStoreIds = row.inStoreId || this.ids;
      this.$modal.confirm('是否确认删除入库编号为"' + inStoreIds + '"的数据项？').then(function() {
        return delStore(inStoreIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/store/export', {
        ...this.queryParams
      }, `store_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
